package com.dragon.modules.yuegou.model.order;

import com.dragon.modules.yuegou.entity.order.OrderItemEntity;
import com.dragon.modules.yuegou.enums.SkuType;
import com.dragon.modules.yuegou.enums.order.OrderItemSellerType;
import com.dragon.modules.yuegou.enums.order.OrderItemTemplateType;
import com.dragon.modules.yuegou.enums.order.OrderStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单物品模型类
 * 
 * @author pbuilder generated
 * @date 2019-04-03 13:57:50
 * @company sgy
 * @version 2.0
 * @see Length 长度限制
 * @see NotNull 对象非空
 * @see NotEmpty 字符串、集合、表、数组非空
 */
public class OrderItem extends OrderItemEntity {

	private LocalDateTime orderTime;

	@ApiModelProperty("状态")
	private OrderStatus status;

	@ApiModelProperty("规格名称")
	private String attributeName;

	@ApiModelProperty("规格值")
	private String optionValue;

	@ApiModelProperty("规格属性值")
	private List<String> attrs;

	private String attrsStr;

	@ApiModelProperty("商品ID")
	private String spuId;

	@ApiModelProperty(value = "优惠券模板ID")
	private String couponTemplateId;

	@ApiModelProperty(value = "优惠券模板名称")
	private String couponTemplateTitle;
	
	@ApiModelProperty(value = "优惠后金额")
	private BigDecimal needPayFee;

	/** 是否使用会员价 */
	@ApiModelProperty("是否使用会员价")
	protected Boolean useMemberPrice;

	// 每一项折扣金额分配
	private List<BigDecimal> itemDiscountList;
	
	// 商户承担金额分配
	private List<BigDecimal> itemMerchantDiscountList;
	
	// 平台承担金额分配
	private List<BigDecimal> itemGroupDiscountList;

	public String getCouponTemplateId() {
		return couponTemplateId;
	}

	public void setCouponTemplateId(String couponTemplateId) {
		this.couponTemplateId = couponTemplateId;
	}

	public String getCouponTemplateTitle() {
		return couponTemplateTitle;
	}

	public void setCouponTemplateTitle(String couponTemplateTitle) {
		this.couponTemplateTitle = couponTemplateTitle;
	}

	public BigDecimal getNeedPayFee() {
		return needPayFee;
	}

	public void setNeedPayFee(BigDecimal needPayFee) {
		this.needPayFee = needPayFee;
	}

	public Boolean getUseMemberPrice() {
		return useMemberPrice;
	}

	public void setUseMemberPrice(Boolean useMemberPrice) {
		this.useMemberPrice = useMemberPrice;
	}

	public List<BigDecimal> getItemDiscountList() {
		return itemDiscountList;
	}

	public void setItemDiscountList(List<BigDecimal> itemDiscountList) {
		this.itemDiscountList = itemDiscountList;
	}

	public List<BigDecimal> getItemMerchantDiscountList() {
		return itemMerchantDiscountList;
	}

	public void setItemMerchantDiscountList(List<BigDecimal> itemMerchantDiscountList) {
		this.itemMerchantDiscountList = itemMerchantDiscountList;
	}

	public List<BigDecimal> getItemGroupDiscountList() {
		return itemGroupDiscountList;
	}

	public void setItemGroupDiscountList(List<BigDecimal> itemGroupDiscountList) {
		this.itemGroupDiscountList = itemGroupDiscountList;
	}

	public String getSpuId() {
		return spuId;
	}

	public void setSpuId(String spuId) {
		this.spuId = spuId;
	}

	public String getAttrsStr() {
		return attrsStr;
	}

	public void setAttrsStr(String attrsStr) {
		this.attrsStr = attrsStr;
	}

	public List<String> getAttrs() {
		return attrs;
	}

	public void setAttrs(List<String> attrs) {
		this.attrs = attrs;
	}

	public LocalDateTime getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(LocalDateTime orderTime) {
		this.orderTime = orderTime;
	}

	public OrderStatus getStatus() {
		return status;
	}

	public void setStatus(OrderStatus status) {
		this.status = status;
	}

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}

	public String getOptionValue() {
		return optionValue;
	}

	public void setOptionValue(String optionValue) {
		this.optionValue = optionValue;
	}

	/**
	 * 构造一个空订单物品模型对象
	 */
	public OrderItem() {
		super();
	}

	/**
	 * 依据实体对象的信息构造一个订单物品模型对象
	 * 
	 * @param entity
	 *            实体对象
	 */
	public OrderItem(OrderItemEntity entity) {
		super(entity);
	}

	/**
	 * 依据其它对象的信息构造一个订单物品模型对象
	 * 
	 * @param other
	 *            其它对象
	 */
	public OrderItem(OrderItem other) {
		super(other);
	}

	@Override
	public String getId() {
		return id;
	}

	/**
	 * 设置订单物品ID
	 * 
	 * @param id
	 *            订单物品ID
	 * @return 当前对象
	 */
	public OrderItem id(String id) {
		this.id = id;
		return this;
	}

	@Override
	public String getOrderNo() {
		return orderNo;
	}

	/**
	 * 设置订单号
	 * 
	 * @param orderNo
	 *            订单号
	 * @return 当前对象
	 */
	public OrderItem orderNo(String orderNo) {
		this.orderNo = orderNo;
		return this;
	}

	@Override
	public String getSkuId() {
		return skuId;
	}

	/**
	 * 设置商品ID
	 * 
	 * @param skuId
	 *            商品ID
	 * @return 当前对象
	 */
	public OrderItem skuId(String skuId) {
		this.skuId = skuId;
		return this;
	}

	@Override
	public SkuType getSkuType() {
		return skuType;
	}

	/**
	 * 设置[全局枚举]商品类型：0-悦卡-YUECARD、1-套餐-PACKAGE、2-商户停车券-PARKCOUPON、3-积分停车券-POINTPARKCOUPON、4-实物-GOODS、5-积分商品-POINTGOODS、6-伪造商品-MOCK
	 * 
	 * @param skuType
	 *            [全局枚举]商品类型：0-悦卡-YUECARD、1-套餐-PACKAGE、2-商户停车券-PARKCOUPON、3-积分停车券-POINTPARKCOUPON、4-实物-GOODS、5-积分商品-POINTGOODS、6-伪造商品-MOCK
	 * @return 当前对象
	 */
	public OrderItem skuType(SkuType skuType) {
		this.skuType = skuType;
		return this;
	}

	@Override
	public String getSkuTitle() {
		return skuTitle;
	}

	/**
	 * 设置商品名
	 * 
	 * @param skuTitle
	 *            商品名
	 * @return 当前对象
	 */
	public OrderItem skuTitle(String skuTitle) {
		this.skuTitle = skuTitle;
		return this;
	}

	@Override
	public BigDecimal getSkuPrice() {
		return skuPrice;
	}

	/**
	 * 设置商品单价
	 * 
	 * @param skuPrice
	 *            商品单价
	 * @return 当前对象
	 */
	public OrderItem skuPrice(BigDecimal skuPrice) {
		this.skuPrice = skuPrice;
		return this;
	}

	@Override
	public BigDecimal getSkuOriginalPrice() {
		return skuOriginalPrice;
	}

	/**
	 * 设置原价
	 * 
	 * @param skuOriginalPrice
	 *            原价
	 * @return 当前对象
	 */
	public OrderItem skuOriginalPrice(BigDecimal skuOriginalPrice) {
		this.skuOriginalPrice = skuOriginalPrice;
		return this;
	}

	@Override
	public BigDecimal getSkuDiscountPrice() {
		return skuDiscountPrice;
	}

	/**
	 * 设置折扣价（用这个价格计算）
	 * 
	 * @param skuDiscountPrice
	 *            折扣价（用这个价格计算）
	 * @return 当前对象
	 */
	public OrderItem skuDiscountPrice(BigDecimal skuDiscountPrice) {
		this.skuDiscountPrice = skuDiscountPrice;
		return this;
	}

	@Override
	public Integer getSkuPoints() {
		return skuPoints;
	}

	/**
	 * 设置商品积分
	 * 
	 * @param skuPoints
	 *            商品积分
	 * @return 当前对象
	 */
	public OrderItem skuPoints(Integer skuPoints) {
		this.skuPoints = skuPoints;
		return this;
	}

	@Override
	public String getSkuMainImages() {
		return skuMainImages;
	}

	/**
	 * 设置商品图片地址
	 * 
	 * @param skuMainImages
	 *            商品图片地址
	 * @return 当前对象
	 */
	public OrderItem skuMainImages(String skuMainImages) {
		this.skuMainImages = skuMainImages;
		return this;
	}

	@Override
	public Integer getSkuUnit() {
		return skuUnit;
	}

	/**
	 * 设置单位，待确定（商品模块）
	 * 
	 * @param skuUnit
	 *            单位，待确定（商品模块）
	 * @return 当前对象
	 */
	public OrderItem skuUnit(Integer skuUnit) {
		this.skuUnit = skuUnit;
		return this;
	}

	@Override
	public String getSkuAttrs() {
		return skuAttrs;
	}

	/**
	 * 设置SKU规格属性 JSON存储
	 * 
	 * @param skuAttrs
	 *            SKU规格属性 JSON存储
	 * @return 当前对象
	 */
	public OrderItem skuAttrs(String skuAttrs) {
		this.skuAttrs = skuAttrs;
		return this;
	}

	@Override
	public Integer getNum() {
		return num;
	}

	/**
	 * 设置数量
	 * 
	 * @param num
	 *            数量
	 * @return 当前对象
	 */
	public OrderItem num(Integer num) {
		this.num = num;
		return this;
	}

	@Override
	public BigDecimal getTotalAmount() {
		return totalAmount;
	}

	/**
	 * 设置总金额（amount = price * num)
	 * 
	 * @param totalAmount
	 *            总金额（amount = price * num)
	 * @return 当前对象
	 */
	public OrderItem totalAmount(BigDecimal totalAmount) {
		this.totalAmount = totalAmount;
		return this;
	}

	@Override
	public BigDecimal getCouponDiscountFee() {
		return super.getCouponDiscountFee();
	}

	@Override
	public void setCouponDiscountFee(BigDecimal couponDiscountFee) {
		super.setCouponDiscountFee(couponDiscountFee);
	}

	@Override
	public Integer getTotalPoints() {
		return totalPoints;
	}

	/**
	 * 设置总积分
	 * 
	 * @param totalPoints
	 *            总积分
	 * @return 当前对象
	 */
	public OrderItem totalPoints(Integer totalPoints) {
		this.totalPoints = totalPoints;
		return this;
	}

	@Override
	public OrderItemSellerType getSellerType() {
		return sellerType;
	}

	/**
	 * 设置[枚举]卖家类型：0-平台-PLATFORM、1-集团-GROUP、2-商圈-BUSD、3-商户-MERCHANT
	 * 
	 * @param sellerType
	 *            [枚举]卖家类型：0-平台-PLATFORM、1-集团-GROUP、2-商圈-BUSD、3-商户-MERCHANT
	 * @return 当前对象
	 */
	public OrderItem sellerType(OrderItemSellerType sellerType) {
		this.sellerType = sellerType;
		return this;
	}

	@Override
	public String getGroupId() {
		return groupId;
	}

	/**
	 * 设置集团ID
	 * 
	 * @param groupId
	 *            集团ID
	 * @return 当前对象
	 */
	public OrderItem groupId(String groupId) {
		this.groupId = groupId;
		return this;
	}

	@Override
	public String getBusdId() {
		return busdId;
	}

	/**
	 * 设置商圈ID
	 * 
	 * @param busdId
	 *            商圈ID
	 * @return 当前对象
	 */
	public OrderItem busdId(String busdId) {
		this.busdId = busdId;
		return this;
	}

	@Override
	public String getMerchantId() {
		return merchantId;
	}

	/**
	 * 设置商户ID
	 * 
	 * @param merchantId
	 *            商户ID
	 * @return 当前对象
	 */
	public OrderItem merchantId(String merchantId) {
		this.merchantId = merchantId;
		return this;
	}

	@Override
	public String getTemplateId() {
		return templateId;
	}

	/**
	 * 设置模板ID
	 * 
	 * @param templateId
	 *            模板ID
	 * @return 当前对象
	 */
	public OrderItem templateId(String templateId) {
		this.templateId = templateId;
		return this;
	}

	@Override
	public OrderItemTemplateType getTemplateType() {
		return templateType;
	}

	/**
	 * 设置[枚举]模板类型：0-优惠券-COUPON、1-悦卡-YUECARD
	 * 
	 * @param templateType
	 *            [枚举]模板类型：0-优惠券-COUPON、1-悦卡-YUECARD
	 * @return 当前对象
	 */
	public OrderItem templateType(OrderItemTemplateType templateType) {
		this.templateType = templateType;
		return this;
	}

	@Override
	public LocalDateTime getPickupStartTime() {
		return pickupStartTime;
	}

	/**
	 * 设置提货开始时间（为空表示无限制）
	 * 
	 * @param pickupStartTime
	 *            提货开始时间（为空表示无限制）
	 * @return 当前对象
	 */
	public OrderItem pickupStartTime(LocalDateTime pickupStartTime) {
		this.pickupStartTime = pickupStartTime;
		return this;
	}

	@Override
	public LocalDateTime getPickupExpireTime() {
		return pickupExpireTime;
	}

	/**
	 * 设置提货超时时间（为空表示永不超时）
	 * 
	 * @param pickupExpireTime
	 *            提货超时时间（为空表示永不超时）
	 * @return 当前对象
	 */
	public OrderItem pickupExpireTime(LocalDateTime pickupExpireTime) {
		this.pickupExpireTime = pickupExpireTime;
		return this;
	}

	private static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * 解码skuAttrs
	 */
	public void decodeSkuAttrs() {
		try {
			if (!StringUtils.isEmpty(this.getSkuAttrs())) {
				List<String> list = new ArrayList<>();
				Map<String, String> mapTypes = MAPPER.readerFor(Map.class).readValue(this.getSkuAttrs());
				list.addAll(mapTypes.values());
				this.setAttrs(list);
			}
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

}
